package com.ldzy.controller;

import com.github.pagehelper.PageInfo;
import com.ldzy.pojo.Buy;
import com.ldzy.pojo.Client;
import com.ldzy.pojo.Medicine;
import com.ldzy.pojo.User;
import com.ldzy.service.BuyService;
import com.ldzy.service.ClientService;
import com.ldzy.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Controller
public class BuyController {
    @Autowired
    BuyService buyService;
    @Autowired
    ClientService clientService;
    @Autowired
    MedicineService medicineService;
    @RequestMapping("/searchBuy")//查询订单
    public ModelAndView searchBuy(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size){
        List<Buy> list = buyService.search(page,size);
        PageInfo pageInfos=new PageInfo(list);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("buy");
        modelAndView.addObject("pageInfos",pageInfos);
        return modelAndView;
    }
    @RequestMapping("/add")//点击添加订单，跳转到添加订单页面
    public String add(){
        return "/staff/addBuy";
    }
    @RequestMapping("/addBuy")//添加订单功能
    public void addStaff(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        String name=req.getParameter("name");
        int age=Integer.parseInt(req.getParameter("age"));
        String[] sexs=req.getParameterValues("sex");
        String phone=req.getParameter("phone");
        String csymptom= req.getParameter("csymptom");
        int mid=Integer.parseInt(req.getParameter("mid"));
        int num=Integer.parseInt(req.getParameter("num"));
        if (Objects.equals(name, "") ||name==null||Objects.equals(age, "")
                ||Objects.equals(sexs, "")||Objects.equals(phone, "")
                ||Objects.equals(csymptom, "")||Objects.equals(mid, "0")){
            req.setAttribute("msg","姓名、年龄、症状、电话、不能为空,购买药品编号不能为0");
            req.getRequestDispatcher("admin/error.jsp").forward(req,resp);
            return;
        }
        Client client=new Client();
        client.setAge(age);
        client.setName(name);
        client.setPhone(phone);
        for (String sex : sexs) {
            client.setSex(sex);
        }
        clientService.add(client);//先添加一个顾客信息
        int cid=0;
        List<Client> clients=clientService.getAll();
        for (Client cc:clients){
            cid=cc.getCid();
        }
        User user=(User) session.getAttribute("user_session");
            Buy buy=new Buy();
            buy.setCid(cid);
            buy.setUid(user.getUid());
            buy.setMid(mid);
            buy.setCsymptom(csymptom);
            buy.setNum(num);
        Medicine medicine= medicineService.get(mid);
        int j;
        int count=medicine.getCount()-num;
        medicine.setCount(count);
        medicineService.updateById(medicine);
        j=buyService.add(buy);//添加一个订单信息
        if (j>0){
            req.setAttribute("msg","操作成功");
            req.getRequestDispatcher("admin/error.jsp").forward(req,resp);
            return;
        }
        else {
            req.setAttribute("msg","操作失败");
            req.getRequestDispatcher("admin/error.jsp").forward(req,resp);
            return;
        }
    }
}
